package com.lipuwater.agent.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;

import com.lipuwater.agent.R;
import com.lipuwater.agent.app.base.ui.BaseLayoutActivity;
import com.lipuwater.agent.ui.widget.JSBridgeWebView;
import com.lipuwater.agent.utils.jsbridge.JsBridgeListener;
import com.lipuwater.agent.utils.jsbridge.JsBridgeType;
import com.umeng.UMUtils;
import com.video.JCVideoPlayer;
import com.video.JCVideoPlayerStandard;

import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;


public class WebViewActivity extends BaseLayoutActivity implements View.OnClickListener, JsBridgeListener {
    @Bind(R.id.jsbridge_webciew)
    JSBridgeWebView jsbridgeWebciew;
    private String url;
    private String title;

    private String shareTitle, shareContent, shareLogo, shareUrl;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setMyContentView(R.layout.activity_webview);
        viewToolbarShadow.setVisibility(View.VISIBLE);
        ButterKnife.bind(this);
        init();
    }

    private void init() {
        Intent intent = this.getIntent();
        url = intent.getStringExtra("url");
        title = intent.getStringExtra("title");
        if (title != null && !title.equals("")) {
            if (title.length() > 8) {
                title = title.substring(0, 8) + "...";
            }
        }
        toolbarTitle.setText(title);
        layout_left.setOnClickListener(this);
        toolbar_left_image.setImageResource(R.drawable.icon_aleft);
        jsbridgeWebciew.setJsBridge(this);
        jsbridgeWebciew.loadUrl(url);
    }

    @Override
    public void results(final JsBridgeType JsType, final Object object) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                final Map<String, Object> map = (Map<String, Object>) object;
                if (JsType == JsBridgeType.VIDEO) {
                    JCVideoPlayerStandard.startFullscreen(WebViewActivity.this, JCVideoPlayerStandard.class, map.get("url").toString(), map.get("title").toString());
                } else if (JsType == JsBridgeType.OPENSHARE) {
                    if ((boolean) (map.get("share"))) {
                        toolbar_right_image.setImageResource(R.drawable.icon_share_blue);
                        toolbar_right_image.setVisibility(View.VISIBLE);
                        layout_right.setOnClickListener(WebViewActivity.this);
                        layout_right.setVisibility(View.VISIBLE);
                    }
                } else if (JsType == JsBridgeType.SHARE) {
                    shareTitle = map.get("title").toString();
                    shareContent = map.get("content").toString();
                    shareLogo = map.get("logo").toString();
                    shareUrl = map.get("url").toString();
                }
            }
        });
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.layout_left:
                if (!jsbridgeWebciew.canGoBack()) {
                    finish();
                } else {
                    jsbridgeWebciew.goBack();
                }
                break;
            case R.id.layout_right:
                UMUtils.share(WebViewActivity.this, shareTitle, shareContent, shareUrl + "?memberId=" + id, shareLogo);
                break;
        }
    }

    //返回的时候返回到webview的前一页而不是后腿
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (!jsbridgeWebciew.canGoBack()) {
                if (JCVideoPlayer.backPress()) {
                    //如果在播放视频就直接return,退出视频
                    return true;
                }
                finish();
            } else {
                jsbridgeWebciew.goBack();
            }
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onPause() {
        super.onPause();
        JCVideoPlayer.releaseAllVideos();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        jsbridgeWebciew.destroyWebView();
        ButterKnife.unbind(this);
    }


}